﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using VolunteerManager.Model;

namespace VolunteerManager
{
    public class Tool
    {
        public static string MD5Hash(string fileName)
        {
            System.Security.Cryptography.HashAlgorithm md5 = System.Security.Cryptography.MD5.Create();
            return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(fileName))).Replace("-", "");
        }
    }

    [ValueConversion(typeof(string), typeof(BitmapImage))]
    public class PathToImageSourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string dp = Config.PhotoRoot + "nomal.jpeg";
            string path = dp;
            if (value != null && value.ToString() != "")
            {
 
                path = value.ToString();
                if (path.LastIndexOf("\\") > 0) //表示是新选的图片
                {
                    
                }
                else {
                    path = Config.PhotoRoot + path;
                }

                if (!System.IO.File.Exists(path))
                {
                    path = dp;
                }
            }

            BitmapImage image = new BitmapImage(new Uri(path));
            return image;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return "path";
        }
    }

    [ValueConversion(typeof(DateTime), typeof(String))]
    public class DateTimeFormat : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || value.ToString() == "")
            {
                return "";
            }
            else
            {
                DateTime date = (DateTime)value;
                return date.ToShortDateString();
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
